﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Report
{
    [Serializable]
    public class Composite:IItem
    {
        private IList<IItem> cList;
        public string ParentName { get; set; }
        public string FirstLevel { get; set; }
        public string SecondLevel { get; set; }
        public string ContentSummary { get; set; }


        public Composite()
        {
            cList = new List<IItem>();
            ContentSummary = string.Empty;
            FirstLevel = string.Empty;
            SecondLevel = string.Empty;
        }

  
        public string Name { get; set; }
        public DateTime AddDate { get; set; }
        public float Income
        {
            get { return cList.Sum(r => r.Income); }
            set {  }

        }

        public float Cost
        {
            get { return cList.Sum(r => r.Cost); }
            set {  }
        }

        public float Borrow
        {
            get
            {
                return cList.Sum(r => r.Borrow);
            }
            set { }
        }
        public float Lent
        {
            get
            {
                return cList.Sum(r => r.Lent);
            }
            set { }
        }

        public IItem Add(IItem item)
        {
            item.ParentName = Name;
            cList.Add(item);
            return item;
        }

        public IItem Remove(IItem item)
        {
            cList.Remove(item);
            return item;

        }

     

        public IItem GetbyName(string name)
        {
            return Name == name ? this : 
                cList.Select(item => item.GetbyName(name)).FirstOrDefault(r => r != null);
        }

        public IList<IItem> GetChildern()
        {
            return cList;
        }

        public string Comment { get; set; }
        public float ExistingIncome {
            get { return Income - Borrow; }
            set { }
        }

        public float ShouldHaveIncome { get { return ExistingIncome - Borrow + Lent; } set { } }
    }
}